package com.eatme.web.app.coupon.common.constant;

import java.util.Objects;
import java.util.stream.Stream;

/**
 * 有效期类型枚举
 */
public enum PeriodType {
    REGULAR("固定的(固定日期)",1),
    SHIFT("变动的(以领取之日开始计算)",2);

    /**
     * 有效期类型描述
     */
    private String description;

    /**
     * 有效期编码
     */
    private Integer code;

    PeriodType(String description, Integer code) {
        this.description = description;
        this.code = code;
    }


    public String getDescription() {
        return description;
    }

    public Integer getCode() {
        return code;
    }

    public static PeriodType of(Integer code) {

        Objects.requireNonNull(code);

        return Stream.of(values())
                .filter(bean -> bean.code.equals(code))
                .findAny().
                orElseThrow(() -> new IllegalArgumentException(code + "not exists!"));
    }
}
